package com.googlecode.sere.method

import com.googlecode.sere.translation.AcceptedTypes
import com.googlecode.sere.http.{Status, HttpRequest}
import com.googlecode.sere.core.{UrlRegistry, InvocationData}

class GetMethod(uri: String, acceptedTypes: AcceptedTypes)
  extends RestMethod(uri, acceptedTypes) {

  override def process(meta: InvocationData): MethodResponse = {
    val (resourceType, id) = UrlRegistry.matchUrl(uri)
    val ResponseEntity(entity, etag) = resourceType.get(id, meta)
    EntityResponse(Status.Ok, resourceType.serialize(acceptedTypes, entity), etag)
  }
}

object GetMethod {
  def fromRequest(request: HttpRequest) = new GetMethod(request.uri, request.acceptedTypes)
}